<script type="text/javascript">
(function(){
	<%
		local para = {
			wireless = {smart_connect = {}, wifi_2g_config = {}, wifi_5g_config = {}}
		}

		local data = tpl_get_data(para, true)
	-%>

	var wirelessInfo = <%=data%>;

	function init(result){
		var errNo = result[ERR_CODE];
		var ret;
		if (E_NONE == errNo){
			ret = result[K_MODULE];
		}else{
			console.log(result);
			return;
		}

		var smartConnect = ret.wireless.smart_connect;
		var wireless2G = ret.wireless.wifi_2g_config;
		var wireless5G = ret.wireless.wifi_5g_config;
		$("#Ssid2G").val(wireless2G.ssid);
		$("#Ssid5G").val(wireless5G.ssid);
		$("#Pwd2G").val(wireless2G.password);
		$("#Pwd5G").val(wireless5G.password);

		initSwitch("Two2One", smartConnect.enable, function(state, init){
			if (0 == state){
				$("#Wifi5G").show();
				$("#WifiName").html(label.wifi2G);
			}else{
				$("#Wifi5G").hide();
				$("#WifiName").html(label.wifiName);
				closeNote();
			}

			if (!init){
				$("#Ssid5G").val(getStrInMaxByte($("#Ssid2G").val(), 29) + "_5G");
			}
		});
	}

	init(wirelessInfo);

	$("#PreviousStep").click(function(){
		var prev = $("li.current-step").prev().attr("id");
		stateman.go(prev);
	});

	$("#SaveReboot").click(function(){
		if (1 == $("#Two2One").attr("data-value")){
			$("#Ssid5G").val($("#Ssid2G").val());
			$("#Pwd5G").val($("#Pwd2G").val());
		}

		if ($("#Ssid2G").val() == $("#Ssid5G").val()
			&& $("#Pwd2G").val() != $("#Pwd5G").val()){
			showNote(errStr.sameWifiPwd);
			return false;
		}

		if ($("#Pwd2G").val() == "" || $("#Pwd5G").val() == ""){
			showConfirm(errStr.emptyWifiPwd, function(choose){
				if (choose){
					saveWifi();
				}
			});
		}else{
			saveWifi();
		}
	});

	$("#MoreSet").click(function(){
		stateman.go("pc.netState");
	});

	function saveWifi(){
		var para = {};
		para.welcome = {};
		para.welcome.config = {};
		para.welcome.config.guide = "0";
		var smart_connect = {};
		var wifi_2g_config = {};
		var wifi_5g_config = {};
		para.wireless = {};
		para.wireless.smart_connect = smart_connect;
		para.wireless.wifi_2g_config = wifi_2g_config;
		para.wireless.wifi_5g_config = wifi_5g_config;
		smart_connect.enable = $("#Two2One").attr("data-value");
		wifi_2g_config.ssid = $("#Ssid2G").val();
		wifi_2g_config.password = $("#Pwd2G").val();
		wifi_5g_config.ssid = $("#Ssid5G").val();
		wifi_5g_config.password = $("#Pwd5G").val();

		if (errHandle(checkSsid2G(wifi_2g_config.ssid), smart_connect.enable)){
			return;
		}

		if (errHandle(checkPwd2G(wifi_2g_config.password), smart_connect.enable)){
			return;
		}

		if (errHandle(checkSsid5G(wifi_5g_config.ssid), smart_connect.enable)){
			return;
		}

		if (errHandle(checkPwd5G(wifi_5g_config.password), smart_connect.enable)){
			return;
		}

		showLoading(label.saveWifi);
		apiSet(para, function(ret){
			var code = ret[ERR_CODE];
			if (E_NONE == code){
				var time = max(ret[K_MODULE].wireless.smart_connect.wait_time, ret[K_MODULE].wireless.wifi_2g_config.wait_time,
								ret[K_MODULE].wireless.wifi_5g_config.wait_time);
				$.setTimeout(function(){
					closeLoading();
					$("#WifiSet").hide();
					$("#WifiWait").show();
					$("#HelpIcon").hide();
					$("ul.language").hide();
					$("#Connect2G").text($("#Ssid2G").val()).attr("title",$("#Ssid2G").val());
					$("#Connect5G").text($("#Ssid5G").val()).attr("title",$("#Ssid5G").val());
				}, time * 1000);
			}else{
				errHandle(code, smart_connect.enable);
				closeLoading();
			}
		});
	}

	function errHandle(code, smartCode){
		var noteStr = "";
		if (smartCode == 0){
			switch(code){
			case E_NONE:
				return false;
			case E_WL2GSSIDBLANK:
				noteStr = errStr.ssidEmpty2G;
				break;
			case E_WL2GSSIDLEN:
				noteStr = errStr.ssidLength2G;
				break;
			case E_WL2GSSIDILLEGAL:
				noteStr = errStr.ssidInclSpeChar2G;
				break;
			case E_WL2GSSIDPWLEN:
				noteStr = errStr.ssidPwdLength2G;
				break;
			case E_WL2GSSIDPWILLEGAL:
				noteStr = errStr.ssidPwdInclSpeChar2G;
				break;
			case E_WL2GSSIDCONFICTWITHGUEST:
				noteStr = errStr.ssid2GVisSame;
				break;
			case E_WL5GSSIDBLANK:
				noteStr = errStr.ssidEmpty5G;
				break;
			case E_WL5GSSIDLEN:
				noteStr = errStr.ssidLength5G;
				break;
			case E_WL5GSSIDILLEGAL:
				noteStr = errStr.ssidInclSpeChar5G;
				break;
			case E_WL5GSSIDPWLEN:
				noteStr = errStr.ssidPwdLength5G;
				break;
			case E_WL5GSSIDPWILLEGAL:
				noteStr = errStr.ssidPwdInclSpeChar5G;
				break;
			case E_WL5GSSIDCONFICTWITHGUEST:
				noteStr = errStr.ssid5GVisSame;
				break;
			default:
				noteStr = errStr.unknownErr + code;
				break;
			}
		}else{
			switch(code){
			case E_NONE:
				return false;
			case E_WL2GSSIDBLANK:
			case E_WL5GSSIDBLANK:
				noteStr = errStr.ssidEmpty;
				break;
			case E_WL2GSSIDLEN:
			case E_WL5GSSIDLEN:
				noteStr = errStr.ssidLength;
				break;
			case E_WL2GSSIDILLEGAL:
			case E_WL5GSSIDILLEGAL:
				noteStr = errStr.ssidInclSpeChar;
				break;
			case E_WL2GSSIDPWLEN:
			case E_WL5GSSIDPWLEN:
				noteStr = errStr.ssidPwdLength;
				break;
			case E_WL2GSSIDPWILLEGAL:
			case E_WL5GSSIDPWILLEGAL:
				noteStr = errStr.ssidPwdInclSpeChar;
				break;
			case E_WL2GSSIDCONFICTWITHGUEST:
			case E_WL5GSSIDCONFICTWITHGUEST:
				noteStr = errStr.ssidVisSame;
				break;
			default:
				noteStr = errStr.unknownErr + code;
				break;
			}
		}

		showNote(noteStr);

		return true;
	}

	function checkSsid2G(ssid){
		var result = checkSsid(ssid)

		if (result == E_SSID_BLANK){
			return E_WL2GSSIDBLANK;
		}

		if (result == E_SSID_LEN){
			return E_WL2GSSIDLEN;
		}

		return E_NONE;
	}

	function checkPwd2G(password){
		var result = checkWlanPwd(password)

		if ("" == password){
			return E_NONE;
		}

		if (result == E_WIFI_PWD_LEN){
			return E_WL2GSSIDPWLEN;
		}

		if (result == E_WIFI_PWD_ILLEGAL){
			return E_WL2GSSIDPWILLEGAL;
		}

		return E_NONE;
	}

	function checkSsid5G(ssid){
		var result = checkSsid(ssid)

		if (result == E_SSID_BLANK){
			return E_WL5GSSIDBLANK;
		}

		if (result == E_SSID_LEN){
			return E_WL5GSSIDLEN;
		}

		return E_NONE;
	}

	function checkPwd5G(password){
		var result = checkWlanPwd(password)

		if ("" == password){
			return E_NONE;
		}

		if (result == E_WIFI_PWD_LEN){
			return E_WL5GSSIDPWLEN;
		}

		if (result == E_WIFI_PWD_ILLEGAL){
			return E_WL5GSSIDPWILLEGAL;
		}

		return E_NONE;
	}

	var scroll = new NiceScroll("GuideCon");
	var scrollWait = new NiceScroll("WifiWait");
	scroll.init();
	scrollWait.init();
})();
</script>
<style type="text/css">
	#Help{
		width:25%;
	}
	li.current-step h2{
		background-color:#f3335c;
	}
	p.set-con-desc{
		margin-left:0;
		padding-bottom:12px;
		border-bottom:1px solid #FF8000;
	}
	label.desc-title{
		font-size:16px;
	}
	p.desc-tetail{
		color:#909396;
	}
	p.tetail-width{
		width:300px;
	}
	label.title-desc-lbl{
		font-size:14px;
		color:#4F5356;
		margin-left:50px;
	}
	label.tip-lbl{
		font-size:16px;
	}
	ul.set-con-title{
		margin-top:20px;
	}
	i.step-3{
		background:url(/luci-static/images/step3.png?_=20170809165512) no-repeat;
	}
	ul.wifi-list{
		position:relative;
		width:100%;
		text-align:center;
		height:200px;
		top:83px;
	}
	li.wifi-item{
		display:inline-block;
	}
	li.wifi-item div.wifi-icon{
		width:200px;
		height:200px;
		border-radius:50%;
		border:0.5px solid #ddd;
		position:relative;
	}
	li.list-right{
		margin-left:88px;
	}
	li.wifi-item i.icon{
		position:absolute;
		font-size:100px;
		color:#fe8f19;
		top:16px;
		left:50px;
	}
	div.wifi-icon i.band-icon{
		position:absolute;
		font-size:54px;
		color:#fe8f19;
		top:82px;
		left:73px;
	}
	span.wifi-name{
		position:relative;
		font-size:16px;
		color:#3f4044;
		width:152px;
		top:138px;
		display:inline-block;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	div.wifi-tip{
		height:51px;
		text-align:center;
		margin-top:113px;
		color:#3f4044;
		font-size:16px;
	}
	p.list-con-bottom{
		margin-top:19px;
	}
	/*1080p*/
	@media screen and (min-device-width:1920px){
		label.tip-lbl{
			font-size:18px;
		}
		p.desc-tetail{
			width:400px;
		}
		label.title-desc-lbl{
			font-size:18px;
			margin-left:74px;
		}
	}
</style>
<div class="help hidden">
	<ul class="help-content">
		<li class="title">{%helpStr.smartConnect%}</li>
		<li class="content">{%helpStr.smartConnectTip%}</li>
		<li class="title">{%helpStr.ssid%}</li>
		<li class="content">{%helpStr.ssidTip%}</li>
		<li class="title">{%helpStr.wifiPwd%}</li>
		<li class="content">{%helpStr.wifiPwdTip%}</li>
	</ul>
</div>
<div class="head">
	<div class="left-logo">
		<img class="logo" src="/luci-static/images/phicomm.png?_=20170809165512" alt="phicomm" />
		<span class="brand">{%label.phicomm%}</span>
	</div>
	<i id="HelpIcon" class="icon guide-icon icon_help"></i>
</div>
<div id="WifiSet" class="guide">
	<div class="left-image-con center-container">
		<div class="image-con vertical-center">
			<h1 class="guide-title">{%label.wifiSet%}</h1>
			<img class="left-image" src="/luci-static/images/wifi.png?_=20170809165512" alt="{%label.wifiSet%}" />
		</div>
	</div>
	<div id="GuideCon" class="set-con right-set-con center-container">
		<div class="image-con vertical-center">
			<ul class="set-con set-con-switch hidden">
				<p class="set-con-desc">
					<label class="tip-lbl">{%label.twoInone%}</label>
					<span id="Two2One" class="switch">
						<i class="switch-circle icon_select_off"></i>
					</span>
				</p>
				<p class="desc-tetail tetail-width">{%label.wifiRemark%}</p>
			</ul>
			<ul class="set-con-title">
				<label class="title-desc-lbl" id="WifiName">{%label.wifi2G%}</label>
			</ul>
			<ul class="set-con">
				<li class="text-con">
					<label class="desc-lbl">{%label.ssidName%}</label>
					<input id="Ssid2G" class="text hover" maxLength="32" value="" />
				</li>
			</ul>
			<ul class="set-con">
				<li class="text-con">
					<label class="desc-lbl">{%label.password%}</label>
					<input id="Pwd2G" class="text hover" type="password" maxLength="63" value="" /><i class="tip icon-eye" data-value="0"></i>
				</li>
			</ul>
			<div id="Wifi5G">
				<ul class="set-con-title">
					<label class="title-desc-lbl">{%label.wifi5G%}</label>
				</ul>
				<ul class="set-con">
					<li class="text-con">
						<label class="desc-lbl">{%label.ssidName%}</label>
						<input id="Ssid5G" class="text hover" maxLength="32" value="" />
					</li>
				</ul>
				<ul class="set-con">
					<li class="text-con">
						<label class="desc-lbl">{%label.password%}</label>
						<input id="Pwd5G" class="text hover" type="password" maxLength="63" value="" /><i class="tip icon-eye" data-value="0"></i>
					</li>
				</ul>
			</div>
			<div class="err-note hidden">
				<i class="err-icon icon_remind_line"></i>
				<p class="note-str"></p>
			</div>
			<div class="guide-btn-con">
				<input id="PreviousStep" class="btn guide-btn " type="button" value="{%btn.previousStep%}" />
				<input id="SaveReboot" class="btn guide-btn guide-btn-right" type="button" value="{%btn.save%}" />
			</div>
		</div>
	</div>
</div>
<div id="WifiWait" class="guide hidden">
	<ul class="wifi-list">
		<li class="wifi-item">
			<div class="wifi-icon">
				<i class="icon icon_wifi_new" ></i>
				<i class="icon_4g_new band-icon" ></i>
				<span class="wifi-name" id="Connect2G"></span>
			</div>
		</li>
		<li class="wifi-item list-right">
			<div class="wifi-icon">
				<i class="icon icon_wifi_new" ></i>
				<i class="icon_G_new band-icon" ></i>
				<span class="wifi-name" id="Connect5G"></span>
			</div>
		</li>
	</ul>
	<div class="wifi-tip">
		<p>{%label.createSuccess%}</p>
		<p class="list-con-bottom">{%label.manualConnection%}</p>
	</div>
	<div class="btn-con">
		<input id="MoreSet" class="btn btn-save" type="button" value="{%btn.moreSet%}" />
	</div>
</div>
<%+footer%>